package com.ithuameng.admin.app.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ithuameng.admin.app.system.dao.SequenceRuleMapper;
import com.ithuameng.admin.app.system.model.SequenceRule;
import com.ithuameng.admin.app.system.model.admin.qo.SequenceRuleQo;
import com.ithuameng.admin.app.system.service.ISequenceRuleService;
import com.ithuameng.admin.enums.SequenceType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Sequence rule service implementation
 *
 * @author ithuameng
 */
@Transactional
@Service
public class SequenceRuleServiceImpl extends ServiceImpl<SequenceRuleMapper, SequenceRule> implements ISequenceRuleService {

    @Override
    public SequenceRule getSeqRuleByType(SequenceType type) {
        QueryWrapper<SequenceRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SequenceRule.TYPE, type);
        return super.getOne(queryWrapper);
    }

    @Override
    public IPage<SequenceRule> listSequenceRule(Page<SequenceRule> page, SequenceRuleQo sequenceRuleQo) {
        QueryWrapper<SequenceRule> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(sequenceRuleQo.getName())) {
            queryWrapper.like(SequenceRule.NAME, sequenceRuleQo.getName());
        }
        return this.baseMapper.selectPage(page, queryWrapper);
    }
}
